<template>
  <div class="container">
    <!-- 固定导航 -->
    <van-nav-bar fixed left-arrow @click-left="$router.back()" title="小慧同学"></van-nav-bar>

    <!-- 聊天主体区域 -->
    <div class="chat-list">
      <div v-for="(item, index) in list" :key="index">
        <!-- 左侧是机器人小思 -->
        <div class="chat-item left" v-if="item.name === 'xs'">
          <van-image fit="cover" round src="https://img.yzcdn.cn/vant/cat.jpeg" />
          <div class="chat-pao">{{ item.msg }}</div>
        </div>

        <!-- 右侧是当前用户 -->
        <div class="chat-item right" v-else>
          <div class="chat-pao">{{ item.msg }}</div>
          <van-image fit="cover" round :src="userAvatar" />
        </div>
      </div>
    </div>

    <!-- 对话区域 -->
    <div class="reply-container van-hairline--top">
      <van-field v-model.trim="word" placeholder="说点什么...">
        <template #button>
          <span @click="send()" style="font-size:17px;color:#999">提交</span>
        </template>
      </van-field>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
// 按需导入 io 方法：调用 io('url') 方法，即可创建 websocket 连接的实例
import { io } from 'socket.io-client'

export default {
  name: 'Chat',
  data() {
    return {
      // 用户输入的聊天内容
      word: '',
      list: [],
      // // 1. 只根据 name 属性，即可判断出这个消息应该渲染到左侧还是右侧
      // { name: 'xs', msg: 'hi，你好！我是小思' },
      // { name: 'me', msg: '我是编程小王子' }
      socket: null
    }
  },
  methods: {
    // 提交按钮的点击事件处理函数
    send() {
      // 如果输入的聊天内容为空，则 return 出去
      if (!this.word) return

      // 向服务器发送消息
      this.socket.emit('send', this.word)
      // 将用户填写的消息存储到list数组中
      this.list.push({ name: 'me', msg: this.word })

      this.word = ''
    }
  },
  created() {
    this.socket = io('ws://www.liulongbin.top:9999')
    // 建立连接的事件
    // this.socket.on('connect', () => console.log('connect: websocket 连接成功！'))
    // 关闭连接的事件
    // this.socket.on('disconnect', () => console.log('disconnect: websocket 连接关闭！'))
    // 接收到消息的事件
    this.socket.on('message', msg => {
      // 把服务器发送过来的消息，存储到 list 数组中
      this.list.push({ name: 'xs', msg: msg })
    })
  },
  beforeDestroy() {
    // 关闭连接
    this.socket.close()

    // 销毁websocket对象
    this.socket = null
  },
  computed: {
    ...mapGetters(['userAvatar']) // 计算头像
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 90px;
  box-sizing: border-box;
  background: #f5f7f9;
  padding: 46px 0 50px 0;
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: top;
        width: 80px;
        height: 80px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 80px;
        max-width: 70%;
        min-height: 80px;
        line-height: 80px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 20px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 27px;
        color: #333;
        &::before {
          content: '';
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
    }
  }
}
.chat-item.right {
  text-align: right;
  .chat-pao {
    margin-left: 0;
    margin-right: 15px;
    &::before {
      right: -6px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left {
  text-align: left;
  .chat-pao {
    margin-left: 15px;
    margin-right: 0;
    &::before {
      left: -5px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 88px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>
